import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 87111
 * Date: 2025-11-04
 * Time: 17:51
 */
public class Demo3 {
    public static void main(String[] args) {
        ExecutorService threadPool = Executors.newCachedThreadPool();
        for (int i = 0; i < 1000; i++) {
            int id = i;
            //使用submit()方法提交任务
            threadPool.submit(() -> {
                System.out.println(Thread.currentThread().getName() + "执行了: " + id);
            });
        }

        //等待所有任务执行完了，才销毁线程池
        threadPool.shutdown();
        //不等待线程池任务执行完，立即销毁线程
        threadPool.shutdownNow();
    }
}
